package com.scanner.demo2;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Scanner;

public class RandomDouble extends RandomDoubles implements Readable {

	private int count;

	public RandomDouble(int count) {
		this.count = count;
	}

	/*
	 * 将输入的内容添加到CharBuffer参数中，或者在没有任何输入的时候返回-1
	 * 
	 * @see java.lang.Readable#read(java.nio.CharBuffer)
	 */
	@Override
	public int read(CharBuffer arg0) throws IOException {
		if (count-- == 0)
			return -1;

		String c = Double.toString(next()) + " ";
		arg0.append(c);
		return arg0.length();
	}

	public static void main(String[] args) {
		Scanner reader = new Scanner(new RandomDouble(47));
		while (reader.hasNextDouble()) {
			System.out.println(reader.nextDouble());
		}
	}

}
